
/**
 *  获取域名
 */

var dominName=(function () {
    return location.href.match(/(http[s]?\:\/\/)(.*?)\//)[0]
})();

/**
 *  根据域名返回当前环境
 */

var environment=(function () {
    var host = location.href.match(/(http[s]?\:\/\/)(.*?)\//),
        hostNames = {
            "one.cpic.com.cn": "prod",
            "localhost": "localhost",
            "47.103.143.153": "dev"
        };

    host = host ? host[2] : "";

    return hostNames[host] || "localhost";
})();

//封装的方法------------------------------------------------------------
/**
 * setLocalStorage
 */
function setLocalStorage(key, value, isJson) {
    if (window.localStorage) {
        if (isJson) {
            value = JSON.stringify(value);
        }
        window.localStorage[key] = value;
    } else {
        console.log("当前浏览器不支持localStorage");
    }
};
/**
 * getLocalStorage
 */
function getLocalStorage (key, isJson) {
    if (window.localStorage) {
        var value = window.localStorage[key] || "";
        if (isJson && value) {
            value = JSON.parse(value);
        }
        console.log("获取localStorage数据key=" + key + ",value=" + value);
        return value;
    } else {
        console.log("当前浏览器不支持localStorage");
    }
};
/**
 * removeLocalStorage
 */
function removeLocalStorage(key) {
    if (window.localStorage) {
        window.localStorage.removeItem(key);
    } else {
        console.log("当前浏览器不支持localStorage");
    }
};
/**
 * setSessionStorage
 */
function setSessionStorage(key, value, isJson) {
    var _wkey = "sessionStorage";
    if (/mqqbrowser/i.test(window.navigator.userAgent)) {
        //qq手机浏览器不支持sessionStorage多页面共享
        _wkey = "localStorage";
    }
    console.log("设置sessionStorage数据key=" + key + ",是否为json数据:" + (isJson ? "true" : "false"));
    if (window[_wkey]) {
        if (isJson) {
            value = JSON.stringify(value);
        }
        console.log("设置sessionStorage数据key=" + key + ",value=" + value);
        window[_wkey][key] = value;
    } else {
        console.log("当前浏览器不支持sessionStorage");
    }
};
/**
 * getSessionStorage
 */
function getSessionStorage(key, isJson) {
    var _wkey = "sessionStorage";
    if (/mqqbrowser/i.test(window.navigator.userAgent)) {
        _wkey = "localStorage";
    }
    console.log("获取sessionStorage数据key=" + key + ",是否为json数据:" + (isJson ? "true" : "false"));
    if (window[_wkey]) {
        var value = window[_wkey][key] || "";
        if (isJson && value) {
            value = JSON.parse(value);
        }
        console.log("获取sessionStorage数据key=" + key + ",value=" + value);
        return value;
    } else {
        console.log("当前浏览器不支持sessionStorage");
    }
};
/**
 * removeSessionStorage
 */
function removeSessionStorage(key) {
    var _wkey = "sessionStorage";
    if (/mqqbrowser/i.test(window.navigator.userAgent)) {
        _wkey = "localStorage";
    }
    console.log("移除sessionStorage数据key=" + key);
    if (window[_wkey]) {
        window[_wkey].removeItem(key);
    } else {
        console.log("当前浏览器不支持sessionStorage");
    }
};
/**
 * getCookie
 */
function getCookie (key) {
    console.log("获取cookie值,当前域为" + document.domain + ",key为" + key);
    var arr, reg = new RegExp("(^| )" + key + "=([^;]*)(;|$)");
    arr = document.cookie.match(reg);
    if (arr) {
        console.log("获取cookie值,\r\nkey:" + key + "\r\nvalue:" + arr[2]);
        return unescape(arr[2]);
    } else {
        console.log("获取cookie值,不存在key为" + key + "的cookie");
        return null;
    }
};
/**
 * setCookie
 */
function setCookie(params) {
    params = params || {};
    //params=name, value,domain,expTime分钟
    console.log("设置cookie值,cookie_name为" + params.name);
    var exp = new Date();
    var expires = "";
    if (params.expTime) {
        params.expTime = params.expTime * 60 * 1000;
        exp.setTime(exp.getTime() + params.expTime);
        expires = ";expires=" + exp.toGMTString();
    }
    if (!params.domain) {
        params.domain = document.domain;
    }

    document.cookie = params.name + "="
        + params.value + "; path=/; domain=" + params.domain + ";" + expires;
};

/**
 * removeCookie
 */
function removeCookie (p) {
    var dp = {
        domain: "",
        name: "",
        path: "/"
    };
    if (typeof p == 'object') {
        this.extend(dp, p);
    } else if (typeof p == 'string') {
        dp.name = p;
    }
    if (!dp.domain) {
        dp.domain = document.domain;
    }


    console.log("移除cookie值,当前域为" + dp.domain + ",path为" + dp.path + ",key为" + dp.name);
    var exp = new Date("2000", "1", "1");
    var cval = this.getCookie(dp.name);
    if (cval != null) {
        console.log("成功移除key为" + dp.name + "的cookie值");
        document.cookie = dp.name + "=" + cval + ";domain=" + dp.domain + ";path=" + dp.path + ";expires=" + exp.toGMTString();
    } else {
        console.log("移除cookie值,不存在key为" + dp.name + "的cookie");
    }
};



/**
 * 得到地址栏参数
 * @param name 参数名称
 * @param urls 从指定的urls获取参数
 * @returns string
 */
function getQueryString (name, urls) {
    urls = urls || window.location.href;
    //删除锚点数据
    if (/\#.*?\&/.test(urls)) {
        urls = urls.substring(0, urls.lastIndexOf("#"));
    }
    if (urls && urls.indexOf("?") > -1) { urls = urls.substring(urls.indexOf("?") + 1); }
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = urls ? urls.match(reg) : window.location.search.substr(1).match(reg);
    if (r != null && r[2] != "") {
        var ms = r[2].match(/(\<)|(\>)|(%3C)|(%3E)/g);
        if (ms && ms.length >= 4) {
            //如果检测到有2对及以上开始和结束尖括号
            r[2] = r[2].replace(/(\<)|(%3C)/g, "");
        }
        return r[2];
    }
    return null;
};



//页面跳转地址-------------------------------------------------------
//跳转到页面
var pageAdress={
    '登陆':function() {
        if(environment == 'prod') {
            return 'xxxx';
        }else if(environment == 'dev'){
            return 'http://47.103.143.153/views/html/login.html';
        }else{
            return '../login.html';
        }
    }(),
    '首页':function() {
        if(environment == 'prod') {
            return 'xxxx';
        }else if(environment == 'dev'){
            return 'http://47.103.143.153/views/html/productdetail.html';
        }else{
            return '../html/productdetail.html';
        }
    }(),
    '产品中心':function() {
        //if(environment == 'prod') {
        //    return 'xxxx';
        //}
        //return 'https://www.baidu.com';
        //alert('产品中心')
    }(),
    '保险公司':function() {
        //if(environment == 'prod') {
        //    return 'xxxx';
        //}
        //return 'https://www.baidu.com';
        //alert('保险公司')
    }(),
    '保单查询':function() {
        //if(environment == 'prod') {
        //    return 'xxxx';
        //}
        //return 'https://www.baidu.com';
        //alert('保单查询')
    }(),
    '投保问题':function() {
        //if(environment == 'prod') {
        //    return 'xxxx';
        //}
        //return 'https://www.baidu.com';
        //alert('保单查询')
    }(),
    '问题反馈':function() {
        //if(environment == 'prod') {
        //    return 'xxxx';
        //}
        //return 'https://www.baidu.com';
        //alert('问题反馈')
    }(),
    '我的订单':function() {
        //if(environment == 'prod') {
        //    return 'xxxx';
        //}
        //return 'https://www.baidu.com';
        //alert('问题反馈')
    }(),
    '消息中心':function() {
        //if(environment == 'prod') {
        //    return 'xxxx';
        //}
        //return 'https://www.baidu.com';
        //alert('问题反馈')
    }(),
    '修改密码':function() {
        //if(environment == 'prod') {
        //    return 'xxxx';
        //}
        //return 'https://www.baidu.com';

    }(),
}

function gotoUrl(val){
    window.location.href=pageAdress[val]
}